<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\ArticleRepository;

class ArticleController extends ManageBaseController
{

    private $articleRepo = NULL;

    public function __construct(
        ArticleRepository $articleRepo
    )
    {
        parent::__construct();
        $this->articleRepo = $articleRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $res = $this->articleRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($res);
    }

    public function update($id = null)
    {
//        'summary',
        $data = request()->only(['id', 'title', 'author_name', 'status',  'content', 'category_id', 'thumb', 'sort']);
        if (!$id) {
            $data['create_user_id'] = UID;
        }
        $data['update_user_id'] = UID;

        $last_id = $this->articleRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(empty($data['id']) ? "新增失败" : "更新失败");
        }

        $res = $this->articleRepo->getOne($last_id);
        return retSuc($res);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->articleRepo->getOne($id);
        return retSuc($res);
    }

    public function delete($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return retFail("参数错误");
        }

        return $this->articleRepo->deleteArticle($id);
    }

}
